const signInBtn = $('#signIn')
const signUpBtn = $('#signUp')
const container = $('.container')
baseURL = 'http://localhost:3000/users'

$(function () {
  signInBtn.on('click', () => {
    $('.container').removeClass('right-panel-active')
  })
  signInBtn.click()
  signUpBtn.on('click', () => {
    $('.container').addClass('right-panel-active')
  })

  $('#registerbtn').on('click', function () {
    //获取表单对象
    var regFormValidator = $('#form1').data('bootstrapValidator')
    regFormValidator.validate()
    // 注册
    if (regFormValidator.isValid()) {
      //表单提交的方法、比如ajax提交
      register()
    }
  })

  $('#loginbtn').on('click', function () {
    var loginFormValidator = $('#form2').data('bootstrapValidator')
    if (loginFormValidator.isValid()) {
      //登录
      login()
    }
  })
})

window.onload = function () {
  //页面初始化时，如果帐号密码cookie存在则填充
  if (localStorage.getItem('username')) {
    const logusername = localStorage.getItem('username')
    const logpassword = localStorage.getItem('password')
    $('#logusername').attr('value', logusername)
    $('#logpassword').attr('value', logpassword)
    $('#remember').prop('checked', true)
    var loginFormValidator = $('#form2').data('bootstrapValidator')
    loginFormValidator.validate()
  }
  //复选框勾选状态发生改变时，如果未勾选则清除cookie
  $('body').on('change', '#remember', function () {
    if (!$('#remember').prop('checked')) {
      localStorage.removeItem('username')
      localStorage.removeItem('password')
    }
  })
}
